package top.pmwly.CSRF.hadler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName MyAccessDenimHandler.java
 * @Description 403错误处理
 * @createTime 2021年08月19日 14:31:00
 */
@Component
public class MyAccessDeniedHandler  implements AccessDeniedHandler {
    /**
     * 403的错误处理
     * @param request
     * @param response
     * @param accessDeniedException
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //乱码处理
        response.setContentType("application/json;charset=utf-8");
        //或者这样处理乱码
        response.setHeader("ContentType","application/json;charset=utf-8");
        //返回的信息
        String msg = "{\"status\":\"error\",\"msg\":\"权限不足！\"}";
        //设置状态
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.getWriter().println(msg);
    }
}
